﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using OrganizedIN.BAL;
using OrganizedIN.BussinessServices;
namespace OrganizedIN
{
    public partial class registration : System.Web.UI.Page
    {
        int year, month;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                btnregister.Attributes.Add("onClick", "return CheckValidation()");
                DateTime tnow = DateTime.Now;
                ArrayList AlYear = new ArrayList();
                int i;
                for (i = 1930; i <= 2011; i++)
                    AlYear.Add(i);
                ArrayList AlMonth = new ArrayList();
                for (i = 1; i <= 12; i++)
                    AlMonth.Add(i);
                if (!this.IsPostBack)
                {
                    ddlselectyear.DataSource = AlYear;
                    ddlselectyear.DataBind();
                    ddlselectyear.SelectedValue = tnow.Year.ToString();
                    ddlselectmonth.DataSource = AlMonth;
                    ddlselectmonth.DataBind();
                    ddlselectmonth.SelectedValue = tnow.Month.ToString();
                    year = Int32.Parse(ddlselectyear.SelectedValue);
                    month = Int32.Parse(ddlselectmonth.SelectedValue);
                    BindDays(year, month);
                    ddlselectday.SelectedValue = tnow.Day.ToString();
                }
                txtfrstname1.Focus();
            }
            catch (Exception ex)
            {
            }
        }
        protected void btnregister_Click(object sender, EventArgs e)
        {
            try
            {
                string date;
                BLL registerobj = new BLL();
                UserRegistration setdata = new UserRegistration();
                if (txtlstname1.Text != "" && txtpassword.Text != "" && chkcondition.Checked && ddlselectgender.SelectedItem.Text != "Select" && ddlselectmonth.SelectedItem.Text != "Month:" && ddlselectday.SelectedItem.Text != "Day:" && ddlselectyear.SelectedItem.Text != "year:")
                {
                    if (txtpassword.Text.Trim() == txtconfrmpassword.Text.Trim())
                    {
                        string url = "confirmation.aspx";
                        setdata.UserEmailID = txtemail.Text;
                        setdata.UserFname = txtfrstname1.Text;
                        setdata.UserLname = txtlstname1.Text;
                        setdata.UserGender = ddlselectgender.SelectedItem.Text.Trim();
                        date = ddlselectmonth.SelectedItem.Text.Trim() + '/' + ddlselectday.SelectedItem.Text.Trim() + '/' + ddlselectyear.SelectedItem.Text.Trim();
                        setdata.UserDOB = date;
                        setdata.UserPassword = txtpassword.Text;
                        setdata.UserTimeZone = ddlselecttimezone.SelectedItem.Text.Trim();
                        setdata.UserRegistrationDate = System.DateTime.Now;
                        setdata.UserHearAbtOrganizedin = ddlselectmedium.SelectedItem.Text.Trim();
                        setdata.UserAddComment = txtaddcomment.Text;
                        registerobj.UserRegistrationInsert(setdata);
                        ClearField();
                        ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>OpenConfirmWin('" + url + "')</script>");
                    }
                    else
                    {
                        lblerrmsg.Text = "your password donot match.Plz try again";
                    }
                }
                else
                {
                    lblerrmsg.Text = "Please fill the all fields properly";
                }
            }
            catch (Exception ex)
            {
            }
        }
        protected void ClearField()
        {
            txtfrstname1.Text = "";
            txtlstname1.Text = "";
            txtemail.Text = "";
            txtaddcomment.Text = "";
            chkcondition.Checked = false;
        }
        private bool CheckLeap(int year)
        {
            if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
                return true;
            else
                return false;
        }
        private void BindDays(int year, int month)
        {
            int i;
            ArrayList AlDay = new ArrayList();
            switch (month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    for (i = 1; i <= 31; i++)
                        AlDay.Add(i);
                    break;
                case 2:
                    if (CheckLeap(year))
                    {
                        for (i = 1; i <= 29; i++)
                            AlDay.Add(i);
                    }
                    else
                    {
                        for (i = 1; i <= 28; i++)
                            AlDay.Add(i);
                    }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    for (i = 1; i <= 30; i++)
                        AlDay.Add(i);
                    break;
            }
            ddlselectday.DataSource = AlDay;
            ddlselectday.DataBind();
        }
        public void ddlselectyear_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            try
            {
                year = Int32.Parse(ddlselectyear.SelectedValue);
                month = Int32.Parse(ddlselectmonth.SelectedValue);
                BindDays(year, month);
            }
            catch(Exception ex)
            {
            }
        }
        public void ddlselectmonth_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            try
            {
                year = Int32.Parse(ddlselectyear.SelectedValue);
                month = Int32.Parse(ddlselectmonth.SelectedValue);
                BindDays(year, month);
            }
            catch (Exception ex)
            {
            }
        }
    }
}
